<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include "securities.php";
class Manage_product_owners extends Securities {

	function __construct() {
		parent::__construct('manage_product_owners');
		$this -> form_validation -> set_error_delimiters('<span class="error">', '</span>');
	}
	//Function to display product's owner that register in our website
	public function index()
	{
		$data['title']="Manage user products";
		$data['manage_datatable'] = get_product_owners_manage_table($this -> mod_globals -> select_all(PREFIX_TABLE . 'product_owners'), $this -> uri -> segment(1));
		$this->load->view(B_MASTER.'index',$data);
	}
	//Create new product's owner that want to post product on our website
	public function insert_product_owner(){
		
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}
		
		$data['title'] = "Create product owner account";
		
		$this -> form_validation -> set_rules('first_name', 'First Name', 'required');
		$this -> form_validation -> set_rules('last_name', 'Last Name', 'required');
		$this -> form_validation -> set_rules('username', 'Username', 'required');
		$this -> form_validation -> set_rules('email', 'email', 'required|valid_email');
		$this -> form_validation -> set_rules('password', 'Password', 'trim|min_length[4]|max_length[25|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'Confirm_password', 'trim|min_length[4]|max_length[25]');

		if ($this -> form_validation -> run() === FALSE) {
			
			$this -> load -> view(B_MASTER . 'index', $data);
		}else{
			// Create users table
			
			$data_create_users = array('first_name' => $this -> input -> post('first_name',TRUE), 'last_name' => $this -> input -> post('last_name',TRUE), 'username' => $this -> input -> post('username',TRUE),'email' => $this -> input -> post('email',TRUE), 'password' => sha1($this -> input -> post('password',TRUE)), 'status' =>1, 'created_date' => null);

			$is_saved = $this -> mod_globals -> insert(PREFIX_TABLE . "product_owners", $data_create_users);
			if ($is_saved) {
				$this -> session -> set_flashdata('success', 'New product owner was created success.');
				redirect('manage-product-owners');
			} else {
				$this -> session -> set_flashdata('error', 'New product owner was created fail.');
				redirect('manage-product-owners');
			}
		}
	}
	//Function used to modify information of user product's owner
	public function update_product_owner($owner_id=0){
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}
		
		$data['title'] = "Update product owner account";
		
		$this -> form_validation -> set_rules('first_name', 'First Name', 'required');
		$this -> form_validation -> set_rules('last_name', 'Last Name', 'required');
		$this -> form_validation -> set_rules('username', 'Username', 'required');
		$this -> form_validation -> set_rules('email', 'email', 'required|valid_email');
		$this -> form_validation -> set_rules('password', 'Password', 'trim|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'Confirm_password', 'trim');

		if ($this -> form_validation -> run() === FALSE) {
			$data['owner_account']=$this->mod_globals->select_where(PREFIX_TABLE."product_owners",array('product_owner_id'=>$owner_id));
			$this -> load -> view(B_MASTER . 'index', $data);
		}else{
			if($this -> input -> post('password',TRUE)==""){
				$password=$this->mod_globals->select_string(PREFIX_TABLE."product_owners","password",array('product_owner_id'=>$owner_id));
			}else{
				$password=sha1($this -> input -> post('password',TRUE));
			}
			// Create users table
			$data_update_users = array('first_name' => $this -> input -> post('first_name',TRUE), 'last_name' => $this -> input -> post('last_name',TRUE), 'username' => $this -> input -> post('username',TRUE),'email' => $this -> input -> post('email',TRUE), 'password' => $password, 'status' =>1, 'created_date' => null);

			$is_saved = $this -> mod_globals -> update(PREFIX_TABLE . "product_owners", $data_update_users,array('product_owner_id'=>$owner_id));
			if ($is_saved) {
				$this -> session -> set_flashdata('success', 'New product owner was updated success.');
				redirect('manage-product-owners');
			} else {
				$this -> session -> set_flashdata('error', 'New product owner was updated fail.');
				redirect('manage-product-owners');
			}
		}
	}

	
	//Function used to remove product owner that register to post product in our website
	public function delete_product_owner($owner_id=0){
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$whe_delete_user = array('product_owner_id' => $owner_id);
		$is_deleted = $this -> mod_globals -> delete(PREFIX_TABLE . 'product_owners', $whe_delete_user);
		if ($is_deleted) {
			$this -> session -> set_flashdata('success', 'Product owner data was deleted success.');
			redirect('manage-product-owners');
		} else {
			$this -> session -> set_flashdata('error', 'Product owner data was deleted fail.');
			redirect('manage-product-owners');
		}
	}
	
	//Function used to list products of user that user post depend on user id 
	public function list_owner_products($owner_id=0){
		$owner=$this->mod_globals->select_string(PREFIX_TABLE."product_owners","username",array('product_owner_id'=>$owner_id));
		$data['title']="List product of user : $owner";
		$data['owner_name']=$owner;
		$tbl_product=PREFIX_TABLE."products";
		$tbl_product_images=PREFIX_TABLE."product_images";
		$tbl_product_owner=PREFIX_TABLE."product_owners";
		
		$arr_fields=array("username,product_id,$tbl_product.name AS product_name,price,$tbl_product.status,$tbl_product.created_date,$tbl_product_images.name AS image_name");
		$arr_join=array($tbl_product_owner => array('use_product_owner_id' =>'product_owner_id'),$tbl_product_images=>array("product_id"=>"use_product_id"));
		
		$data['manage_datatable'] = get_list_product_owners_manage_table($this -> mod_globals -> select_join_get_only_fields(PREFIX_TABLE."products", $arr_fields,$arr_join, $join_type = "INNER",  array($tbl_product_images.".priority"=>1,'product_owner_id'=>$owner_id),NULL,"product_id DESC"), $this -> uri -> segment(1));
		
		$this->load->view(B_MASTER.'index',$data);
	}

	//Function used to remove product of user post
	public function delete_owner_product($product_id=0){
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}
		$owner_id=$this->mod_globals->select_string(PREFIX_TABLE."products","use_product_owner_id",array('product_id'=>$product_id));
		$whe_delete_product = array('product_id' => $product_id);
		$is_deleted = $this -> mod_globals -> delete(PREFIX_TABLE . 'products', $whe_delete_product);
		if ($is_deleted) {
			$this -> session -> set_flashdata('success', 'Product data was deleted success.');
			redirect('manage-product-owners/list-owner-products/'.$owner_id);
		} else {
			$this -> session -> set_flashdata('error', 'Product data was deleted fail.');
			redirect('manage-product-owners/list-owner-products/'.$owner_id);
		}
	}
}
